package com.github.xaidanwang.mybatisshardingjdbc.demo.controller;

import com.alibaba.fastjson.JSON;
import com.github.xaidanwang.mybatisshardingjdbc.demo.dao.EventRecordMapper;
import com.github.xaidanwang.mybatisshardingjdbc.demo.entity.EventRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

/**
 * @author wang yi fei
 * @date 2020/6/17 13:25
 */
@RestController
public class RecordInfoController {
	@Autowired
	private EventRecordMapper eventRecordMapper;

	private DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

	@PostMapping(value = "/record")
	public String postRecord(@RequestBody EventRecord record){
		eventRecordMapper.insert(record);
		return "111111";
	}

	@GetMapping(value = "/record")
	public String getAll(String startTime,String endTime){
		SimpleDateFormat  simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		ZoneId zoneId = ZoneId.systemDefault();
		ZonedDateTime start = LocalDateTime.parse(startTime,dateTimeFormatter).atZone(zoneId);
		ZonedDateTime end = LocalDateTime.parse(endTime,dateTimeFormatter).atZone(zoneId);
		Date startdate = Date.from(start.toInstant());
		Date enddate = Date.from(end.toInstant());
		List<EventRecord> recordList = eventRecordMapper.selectByCreateTiem(startdate,enddate);
		System.out.println(recordList.size());
		return JSON.toJSONString(recordList);
	}
}
